const App = getApp();
import Dialog from '../../weapp/dialog/dialog'
// components/list/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    styleP: Boolean,
    strs:String
  },

  /**
   * 组件的初始数据
   */
  data: {
    title: '我的客户',
    currentIndex: 0,
    topData: {},
    totalPrice:'',
    api_url:wx.getStorageSync('api_root'),
    timeData: {},
    deviceList: [],    //列表
    deviceTotal: 0,    //总数
    devicePages: 0,     //总页数
    deviceIndex: 1,      //当前页 
    searchLoading: false, //加载更多
    deviceNoData: false,  //暂无更多
    searchLoadingComplete: false, //加载到底部
    imgList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    deleteItem(e) {
      console.log(e)
      let type = e.currentTarget.dataset.type;
      var _this = this;
      wx.showModal({
        title: '是否确定删除',
        success: (res) => {
          if (res.cancel) {
  
          } else {
            App._post_form(`Customer/orderDel`, {
                order_id:e.currentTarget.dataset.id
              }, function (result) {
                if (result.code === 0) {
                  // let type = _this.data.type;
                  _this.getDetailsList(type)
                }
            })
          }
        }
      })
      /*
      wx.showModal({
        title: '提示',
        content: '确定要删除吗？',
        success: function (sm) {
              App._post_form(`Customer/orderDel`, {
              order_id:e.currentTarget.dataset.id
            }, function (result) {
              if (result.code === 0) {
                // let type = _this.data.type;
                _this.getDetailsList(type)
              }
          })
          }
        })
        */
    },
    onChange(e) {
      this.setData({
        timeData: e.detail,
      });
    },
    handleToDetail(e){
      let {id} = e.currentTarget.dataset
      wx.navigateTo({
        url: `../../pages/user-detail/detail?type=look&id=${id}`,
      })
    },
    handleHomeToReport(e) {
      let type = e.currentTarget.dataset.type
      wx.navigateTo({
        url: `../../pages/report/index?type=${type}`,
      })
    },
    // toDetail: function () {
    //   console.log('1S')
    //   wx.navigateTo({
    //     url: '../../pages/detail/index',
    //   })
    // },
    filterList(arr) {
      for (let index = 0; index < arr.length; index++) {
        arr[index].pic = ''
        arr[index].create_time = arr[index].create_time.slice(5)
        arr[index].create_time = arr[index].create_time.substring(0, arr[index].create_time.length - 3);
        // setInterval(() => {
        // this.countDown(arr[index].rollout_expire_time)
        // }, 1000);

        switch (arr[index].level) {
          case 'A':
            arr[index].pic = '../../images/A.png'
            break;
          case 'B':
            arr[index].pic = '../../images/B.png'
            break;
          case 'C':
            arr[index].pic = '../../images/C.png'
            break;
          case 'D':
            arr[index].pic = '../../images/D.png'
            break;
          case 'T':
            arr[index].pic = '../../images/T.png'
            break;
          default:
            break;
        }
      }
    },
    handleTabChange(e) {
      this.setData({
        deviceIndex: 1,
        deviceList: [],
        title: e.currentTarget.dataset.name,
        currentIndex: e.currentTarget.dataset.index,
        type: e.currentTarget.dataset.type
      })
      this.getCustomersList()
    },
    /**
     * 筛选
     */
    handleScreenList() {
      this.setData({
        deviceIndex: 1,
        deviceList: []
      })
      this.getCustomersList()
    },
  
    /**
     * 明细列表数据
     */
    getDetailsList() {
      let _this = this
      wx.showLoading({
        title: '加载中...',
      })
      App._post_form('Customer/getPartnerOrder', {
        page: _this.data.deviceIndex
      }, function (result) {
        wx.hideLoading()
        _this.filterList(result.data.list)
        _this.setData({
          totalPrice:result.data.total_account
        })
        let Result = result.data.list
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          console.log('没有')
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          _this.setData({
            deviceList: deviceListTemp, //获取数据数组
            deviceNoData: false //“没有数据”隐藏
          });
        }
        console.log(_this.data.deviceList)
      });
    },
    onBottom: function (type) {
      // 页面触底时执行
      var _this = this;

      if (_this.data.deviceIndex < _this.data.devicePages) {
        _this.setData({
          deviceIndex: _this.data.deviceIndex + 1,   //每次触发触底事件，把deviceIndex+1
        });
        console.log(_this.data.deviceIndex)
        _this.getDetailsList(type);
      } else if (_this.data.deviceIndex == _this.data.devicePages) {
        _this.setData({
          searchLoadingComplete: true, //“已加载全部”显示
          searchLoading: false, //"正在载入更多"隐藏
        });
      }
    },

    //图片预览
    preview(event) {
      console.log(event.currentTarget.dataset.src)
      let currentUrl = event.currentTarget.dataset.src
      let imgs = event.currentTarget.dataset.imgs
      imgs = imgs.map(item =>{
        return this.data.api_url+item
      })
      console.log(imgs)
      wx.previewImage({
        current: currentUrl, // 当前显示图片的http链接
        urls: imgs // 需要预览的图片http链接列表
      })
    }
  }
})
